define(['jq', 'common'], () => {
    //首部复用
    $('header').load('../html/header.html')
    //尾部复用
    $('footer').load('../html/footer.html')

    require(['page'], () => {
        setTimeout(() => {
            // 回到顶部
            $('.floor2').click(function () {
                $('html').animate({
                    scrollTop: 0
                }, 500)
            })

            //抢购倒计时
            var endDate = new Date('2022-11-11 00:00:00')
            var spanH = document.querySelector('.h')
            var spanM = document.querySelector('.m')
            var spanS = document.querySelector('.s')
            //获取现在的时间
            setInterval(function () {
                var nowDate = new Date()
                var secondes = parseInt((endDate.getTime() - nowDate.getTime()) / 1000)
                var day = parseInt(secondes / 3600 / 24)
                var hours = parseInt(secondes / 3600 % 24)
                var minutes = parseInt(secondes / 60 % 60)
                var s = parseInt(secondes % 60)
                day < 10 ? day = '0' + day : day
                hours < 10 ? hours = '0' + hours : hours
                minutes < 10 ? minutes = '0' + minutes : minutes
                s < 10 ? s = '0' + s : s
                spanH.innerHTML = hours
                spanM.innerHTML = minutes
                spanS.innerHTML = s
            }, 1000)

            // 商品渲染+加购
            class List {
                constructor() {
                    this.ul = document.querySelector('.list')
                    this.em = document.querySelectorAll('em')[0]
                    this.btn = document.querySelector('button')
                    this.sousuo = document.querySelector('.icon-sousuo')
                    this.search = document.querySelector('.search1')
                    this.init()
                }
                init() {
                    this.request()
                    this.add()
                    //此处再次调用，刷新不会清零
                    this.count()
                    this.Search()
                    // this.sort()
                }
                request() {
                    pAjaxGet({
                            url: '../data/goods.json'
                        })
                        .then(data => {
                            this.datalist = JSON.parse(data).slice(0, 75)
                            this.copyData = JSON.parse(JSON.stringify(this.datalist))
                            this.render(this.datalist)
                        })
                }
                render(data) {
                    console.log('jinlaimei', this.datalist);
                    this.ul.innerHTML = ''
                    document.querySelector('.paging_btn').innerHTML = ''
                    this.datalist.forEach(item => {
                        this.ul.innerHTML += `
                     <li>
                         <div class="left">
                             <img src="${item.img_small_logo}" alt="" class="li" data-id=${item.goods_id}>
                         </div>
                         <div class="right">
                             <p class="name ellipse2">${item.title}</p>
                             <p class="des">秒杀省100元</p>
                             <p class="price"><big>¥${item.current_price}</big><del>¥999</del></p>
                             <button data-id=${item.goods_id}>去加购</button>
                             <span class="iconfont icon-shoucang2" data-id=${item.goods_id}></span>
                         </div>
                     </li>
                 `
                    })
                    $(".list").paging({
                        PageNum: 15, //每页显示数目
                        pageMax: true, //按钮长度是否显示
                        pageMaxHideShow: false, //在最后一个的时候是否隐藏按钮长度
                        pageDownUpHide: false, //到第一个或最后一个是否让上一页或下一页消失
                        pageInput: true, //是否使用文本框输入跳转
                        pagingBtnHide: false, //是否让按钮变为一个
                        pagingBtnPaging: true, //按钮是否分页
                        pagingDisplay: "flex" //显示的属性，弹性盒子还是块化
                    })
                }
                // sort(){
                //     // console.log(this.datalist[1].current_price)
                //     this.goods = document.querySelector('.goods')
                //     console.log(this.goods);
                //     this.goods.addEventListener('click',(e)=>{
                //         console.log(111);
                //         let target = e.target || e.srcElement
                //         //升序
                //         if(target.className == 'icon-xiazhankai'){
                //             console.log(target);
                //             for(let i = 0;i<this.datalist.length-1;i++){
                //                 for(let j=0;j<this.datalist.length-i-1;j++){
                //                     if(this.datalist[i].current_price>this.datalist[i+1].current_price){
                //                         temp = this.datalist[i]
                //                         this.datalist[i] = this.datalist[i+1]
                //                         this.datalist[i+1] = temp
                //                     }
                //                 }
                //             }
                //             this.render()
                //         }
                //降序
                // if(target.className == 'icon-shangzhankai'){
                //     for(let i = 0;i<this.datalist.length-1;i++){
                //         for(let j=0;j<this.datalist.length-i-1;j++){
                //             if(this.datalist[i].current_price<this.datalist[i+1].current_price){
                //                 temp = this.datalist[i]
                //                 this.datalist[i] = this.datalist[i+1]
                //                 this.datalist[i+1] = temp
                //             }
                //         }
                //     }
                //     this.render()
                // }

                add() {
                    this.ul.onclick = (e) => {
                        var e = e || window.event
                        var target = e.target || e.srcElement
                        // console.log(target);
                        if (target.nodeName == 'BUTTON') {
                            let targetId = target.dataset.id
                            //找到当前数据
                            let goods = this.datalist.find((item) => {
                                return item.goods_id == targetId
                            })
                            console.log(goods);
                            let list = JSON.parse(localStorage.getItem('cart')) || []
                            let user = getCookie('username')
                            if (user) {
                                if (list.length > 0) {
                                    let result = list.some((item) => {
                                        return item.goods_id == targetId
                                    })
                                    if (result) {
                                        let index = list.findIndex((item) => {
                                            return item.goods_id == targetId
                                        })
                                        list[index].cart_number++
                                        if (goods.cart_number > goods.goods_number) {
                                            alert('库存不足')
                                        }
                                    } else {
                                        goods.cart_number = 1
                                        list.push(goods)
                                    }
                                } else {
                                    goods.cart_number = 1
                                    list.push(goods)
                                }
                                localStorage.setItem('cart', JSON.stringify(list))
                                //在此调用，点击button才会显示在购物车处
                                this.count()
                            } else {
                                alert('请先登录！')
                                location.href = '../html/login.html'
                            }
                        }
                        if (target.className == 'li') {
                            let targetId = target.dataset.id
                            console.log(targetId);
                            //找到当前数据
                            let goods = this.datalist.find((item) => {
                                return item.goods_id == targetId
                            })
                            console.log(goods);
                            localStorage.setItem('detail', JSON.stringify(goods))
                            location.href = '../html/detail.html'

                        }
                    }
                }
                count() {
                    let list = JSON.parse(localStorage.getItem('cart')) || []
                    let count = 0
                    list.map((item) => {
                        count += item.cart_number
                    })
                    this.em.innerHTML = count
                }
                //搜索
                Search() {

                    this.sousuo.onclick = (item) => {
                        console.log(54646)
                        this.change()
                    }
                    this.search.onkeyup = (e) => {
                        var e = e || window.event
                        var keyCode = e.keyCode || e.which
                        if (keyCode == 13) {
                            this.change()
                        }
                    }
                }

                change() {
                    var str = '南极人'
                    console.log(str.includes(this.search.value), this.copyData)
                    this.datalist = this.copyData.filter((item) => {
                        return item.title.includes(this.search.value)
                        // console.log(item.name.includes(this.search.value), '哈哈哈');
                    })
                    if (!this.search.value) {
                        this.datalist = this.copyData
                    }
                    console.log(this.datalist, 'this.data ');
                    this.render()

                }

            }
            new List()
        }, 500);
    })




})